package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.PersonClassLogic;
import cn.topevery.garden.entity.dto.PersonClassDTO;
import cn.topevery.garden.entity.po.PersonClass;
import cn.topevery.garden.entity.query.PersonClassQuery;
import cn.topevery.garden.entity.vo.PersonClassVO;
import com.github.dozermapper.core.Mapper;
import hw.topevery.framework.SystemConst;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 
 *
 * @author code-generate
 * @date 2021-08-06
 */
@Api(tags = "")
@RestController
@RequestMapping("/personClass")
public class PersonClassController extends BaseController {


    @Autowired
    PersonClassLogic personClassLogic;

    @Autowired
    private Mapper mapper;

    /**
     * 获取分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取页列表")
    @PostMapping("/getList")
    public JsonResult<DataPager<PersonClass>> listPage(@RequestBody PersonClassQuery query) {
        return JsonResult.ok(personClassLogic.listPage(query));
    }

    @ApiOperation("新增/编辑 信息")
    @PostMapping("/save")
    public JsonResult<?> saveOrUpdate(@Valid @RequestBody PersonClassDTO dto) {
        if (dto.getId() == null) {
            dto.setEntityId(UUID.randomUUID().toString());
            dto.setDbCreateDate(LocalDateTime.now());
            dto.setDbCreateId(getUser().id);
            if (StringUtils.isBlank(dto.getParentId())) {
                dto.setParentId(SystemConst.UUID_EMPTY.toString());
            }
        }
        dto.setDbLastUpdateDate(LocalDateTime.now());
        dto.setDbLastUpdateId(dto.getId());
        personClassLogic.saveOrUpdate(getUser().id.toString(), dto);
        return JsonResult.ok("保存成功");

    }

    @GetMapping(value = "/getById")
    @ApiOperation(value = "通过id查询设施类别")
    public JsonResult<?> getById(Integer id) {
        return JsonResult.ok(personClassLogic.getById(id));
    }

    @GetMapping(value = "/delById")
    @ApiOperation(value = "通过id删除")
    public JsonResult<?> delById(String entityId) {
        if (StringUtils.isBlank(entityId)) {
            return JsonResult.error("id不能为空");
        }
        List<PersonClassVO> list = personClassLogic.getListByPid(entityId);
        if (list != null && !list.isEmpty()) {
            return JsonResult.error("该类型有子节点，无法删除");
        }
        personClassLogic.delById(getUser().id, entityId);
        return JsonResult.ok("删除成功");
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        personClassLogic.deleteBatchById(getUser().userId,ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出信息")
    @GetMapping("/export")
    public void export(PersonClassQuery query, HttpServletResponse response) {
        List<PersonClass> list = personClassLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "", "sheet1", PersonClass.class, "", response);
    }

    @ApiOperation("获取key-value数据")
    @GetMapping("/getOptions")
    public JsonResult getOptions() {
        return JsonResult.ok(personClassLogic.getOptions());
    }

    @GetMapping(value = "/getTree")
    @ApiOperation(value = "获取类别树")
    public JsonResult<?> getTree() {
        List<PersonClassVO> list = personClassLogic.getListByPid(SystemConst.UUID_EMPTY.toString());
        if (list != null && !list.isEmpty()) {
            list = buildTree(list);
        }
        return JsonResult.ok(list);
    }

    private List<PersonClassVO> buildTree(List<PersonClassVO> list) {
        List<PersonClassVO> tree = new ArrayList<>();
        PersonClassVO node;
        List<PersonClassVO> child;
        for (PersonClassVO vo : list) {
            node = mapper.map(vo, PersonClassVO.class);
            child = personClassLogic.getListByPid(vo.getEntityId());
            if (child != null && !child.isEmpty()) {
                node.setChildren(buildTree(child));
            }
            tree.add(node);
        }
        return tree;
    }

}
